package com.exam.system.controller;

import com.exam.system.entity.SysUser;
import com.exam.system.service.SysUserServiceimpl;
import com.exam.system.util.BodyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.springframework.http.ResponseEntity.ok;

/**
 * @Author LY
 * @create 2023/10/8 14:23
 * @Description 用户控制层
 */
@RestController
@RequestMapping("/sysUser")
@Slf4j
public class SysUserController {

    @Autowired
    private SysUserServiceimpl sysUserServiceimpl;

    /**
     * 需要sys:user:list权限才能访问
     * @param username
     * @return
     */
    @PreAuthorize("hasAuthority('sys:user:list')")
    @PostMapping("/findSysUser")
    public ResponseEntity findSysUser(@RequestParam("username") String username){
        log.info("获取用户信息：{}",username);
        SysUser sysUser = sysUserServiceimpl.findSysUser(username);
        return ok(BodyUtil.build().ok("获取用户信息成功", sysUser));
    }

    /**
     * 需要有ROLE_admin或者ROLE_sys角色才能访问
     * @param username
     * @return
     */
    @PreAuthorize("hasAnyRole('ROLE_admin','ROLE_sys')")
    @PostMapping("/getPermision")
    public ResponseEntity getPermision(@RequestParam("username") String username){
        log.info("获取用户权限信息：{}",username);
        List<String> list = sysUserServiceimpl.getSysUserPerms(username);
        return ok(BodyUtil.build().ok("获取权限信息成功", list));
    }

    /**
     * 需要sys:user:add权限才能访问
     * @return
     */
    @PreAuthorize("hasAuthority('sys:user:add')")
    @PostMapping("/addSysUser")
    public ResponseEntity addSysUser(){
        return ok(BodyUtil.build().ok("获取成功", ""));
    }
}
